package com.mycc.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mycc.lease.model.entity.*;
import com.mycc.lease.model.enums.ItemType;
import com.mycc.lease.web.admin.mapper.*;
import com.mycc.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mycc.lease.web.admin.vo.attr.AttrValueVo;
import com.mycc.lease.web.admin.vo.graph.GraphVo;
import com.mycc.lease.web.admin.vo.room.RoomDetailVo;
import com.mycc.lease.web.admin.vo.room.RoomItemVo;
import com.mycc.lease.web.admin.vo.room.RoomQueryVo;
import com.mycc.lease.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
@Autowired
private RoomInfoMapper mapper;
@Autowired
private RoomInfoMapper roomInfoMapper;
@Autowired
private GraphInfoMapper graphInfoMapper;
@Autowired
private ApartmentInfoMapper apartmentInfoMapper;
@Autowired
private AttrValueMapper attrValueMapper;
@Autowired
private RoomFacilityMapper roomFacilityMapper;
@Autowired
private RoomLabelMapper roomLabelMapper;
@Autowired
private RoomPaymentTypeMapper roomPaymentTypeMapper;
@Autowired
private RoomLeaseTermMapper leaseTermMapper;
@Autowired
private GraphInfoService graphInfoService;
@Autowired
private RoomAttrValueService roomAttrValueService;
@Autowired
private RoomLabelService roomLabelService;
@Autowired
private RoomFacilityService roomFacilityService;
@Autowired
private RoomPaymentTypeService roomPaymentTypeService;
@Autowired
private RoomLeaseTermService roomLeaseTermService;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        boolean b = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);
        if(!b){
//            删除图片
            LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(graphInfoWrapper);
//            删除属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueWrapper = new LambdaQueryWrapper<>();
            roomAttrValueWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueWrapper);
//            删除标签
            LambdaQueryWrapper<RoomLabel> roomLabelWrapper = new LambdaQueryWrapper<>();
            roomLabelWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(roomLabelWrapper);
//            删除配套
            LambdaQueryWrapper<RoomFacility> roomInfoWrapper = new LambdaQueryWrapper<>();
            roomInfoWrapper.eq(RoomFacility::getId, roomSubmitVo.getId());
            roomFacilityService.remove(roomInfoWrapper);
//            支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeWrapper);
//            租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermWrapper);
        }
//        保存图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        if(!CollectionUtils.isEmpty(graphVoList)){
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfos.add(graphInfo);
            }
        }
        graphInfoService.saveOrUpdateBatch(graphInfos);
//        保存属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        ArrayList<RoomAttrValue> attrValues = new ArrayList<>();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = new RoomAttrValue();
                roomAttrValue.setRoomId(roomSubmitVo.getId());
                roomAttrValue.setAttrValueId(attrValueId);
                attrValues.add(roomAttrValue);
            }
        }
        roomAttrValueService.saveOrUpdateBatch(attrValues);

//        保存标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        ArrayList<RoomLabel> roomLabels = new ArrayList<>();
        if(!CollectionUtils.isEmpty(labelInfoIds)){
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = new RoomLabel();
                roomLabel.setRoomId(roomSubmitVo.getId());
                roomLabel.setLabelId(labelInfoId);
                roomLabels.add(roomLabel);
            }
        }
        roomLabelService.saveOrUpdateBatch(roomLabels);
//        保存配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility = new RoomFacility();
                roomFacility.setRoomId(roomSubmitVo.getId());
                roomFacility.setFacilityId(facilityInfoId);
                roomFacilities.add(roomFacility);
            }
        }
        roomFacilityService.saveOrUpdateBatch(roomFacilities);
//        保存支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        ArrayList<RoomPaymentType> lists = new ArrayList<>();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType = new RoomPaymentType();
                roomPaymentType.setRoomId(roomSubmitVo.getId());
                roomPaymentType.setPaymentTypeId(paymentTypeId);
                lists.add(roomPaymentType);
            }
        }
        roomPaymentTypeService.saveOrUpdateBatch(lists);
//        保存租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
                roomLeaseTerm.setRoomId(roomSubmitVo.getId());
                roomLeaseTerm.setLeaseTermId(leaseTermId);
                roomLeaseTerms.add(roomLeaseTerm);
            }
        }
        roomLeaseTermService.saveOrUpdateBatch(roomLeaseTerms);

    }

    @Override
    public IPage<RoomItemVo> pageItem(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page, queryVo);

    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        List<GraphVo> graphInfo = graphInfoMapper.selectGraphByItemTypeAndItemId(ItemType.ROOM, id);
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        List<AttrValueVo> attrValues = attrValueMapper.selectAttrValueByRoomId(id);
        List<FacilityInfo> facilityInfos = roomFacilityMapper.selectFacilityByRoomId(id);
        List<LabelInfo> labelInfos = roomLabelMapper.selectLabelByRoomId(id);
        List<PaymentType> paymentTypes = roomPaymentTypeMapper.selectPaymentTypeByRoomId(id);
        List<LeaseTerm> leaseTerms = leaseTermMapper.selectLeaseTermByRoomId(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphInfo);
        roomDetailVo.setAttrValueVoList(attrValues);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setLabelInfoList(labelInfos);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setLeaseTermList(leaseTerms);

        return roomDetailVo;


    }

    @Override
    public void removeRoomById(Long id) {
//        删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
//        删除属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
//        删除标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
//        删除配套
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
//        删除支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
//        删除租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);


    }
}




